﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace QuanLyKhoHang
{
    public partial class frmMain : Form
    {
        public frmMain()
        {
            InitializeComponent();
        }

        #region Các hàm xử lý login và phân quyền
        private void DisableMenuLogin(bool logged)
        {
            itemDangNhap.Enabled = !logged;
            itemDangXuat.Enabled = logged;
            itemDoiMatKhau.Enabled = logged;
            itemQuanLy.Enabled = logged;
            itemBaoCao.Enabled = logged;
            //itemTimKiem.Enabled = logged;
            itemThongTin.Enabled = logged;
        }
        #endregion
        private void thoátToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void đăngNhậpToolStripMenuItem_Click(object sender, EventArgs e)
        {
            frmDangNhap dangnhap = new frmDangNhap();
            dangnhap.Show();
            DisableMenuLogin(true);
        }

        private void đổiMậtKhẩuToolStripMenuItem_Click(object sender, EventArgs e)
        {
            frmDoiMatKhau doiMK = new frmDoiMatKhau();
            doiMK.Show();
        }

        private void đăngXuấtToolStripMenuItem_Click(object sender, EventArgs e)
        {
            frmDangNhap dangnhap = new frmDangNhap();
            dangnhap.Show();
            DisableMenuLogin(false);
        }

        private void frmMain_Load(object sender, EventArgs e)
        {
            DisableMenuLogin(false);
        }

        private void quảnLýKhoToolStripMenuItem_Click(object sender, EventArgs e)
        {
            frmQuanLyKho qlKho = new frmQuanLyKho();
            qlKho.Show();
        }

        private void itemNhanVien_Click(object sender, EventArgs e)
        {
            frmInNhanVien nv = new frmInNhanVien();
            nv.Show();
        }

        private void itemHangHoa_Click(object sender, EventArgs e)
        {
            frmInHang hang = new frmInHang();
            hang.Show();
        }

        private void thôngTinToolStripMenuItem_Click(object sender, EventArgs e)
        {
            frmThongTin tt = new frmThongTin();
            tt.Show();
        }

        private void itemXuatKho_Click(object sender, EventArgs e)
        {

        }

        private void itemNhapKho_Click(object sender, EventArgs e)
        {
            frmThemHang themhang = new frmThemHang();
            themhang.Show();
        }

        private void itemQuanLyNhanVien_Click(object sender, EventArgs e)
        {
            frmQuanLyNhanVien nv = new frmQuanLyNhanVien();
            nv.Show();
        }

        private void itemBackup_Click(object sender, EventArgs e)
        {
            try
            {
                string sqlBackup = "BACKUP DATABASE [QLKH] TO DISK='D:\\backupQLKH.bak'";
                SqlConnection con = new SqlConnection("server = TRANCUNG-LAP; database =QLKH; user id = sa; password= 123456;");
                con.Open();
                SqlCommand cmd = new SqlCommand(sqlBackup, con);
                cmd.ExecuteNonQuery();
                con.Close();
                MessageBox.Show("Đã backup cơ sở dữ liệu");
            }
            catch (SqlException ex)
            {
                MessageBox.Show(ex.Message, "Backup Database");
                return;
            }
        }

        private void itemRestore_Click(object sender, EventArgs e)
        {
            try
            {
                string path = "D:\\backupQLKH.bak";
                string sqlRestore = "Use master Restore Database [QLKH] from disk='" + path + "'";
                SqlConnection con = new SqlConnection("server = TRANCUNG-LAP; database =QLKH; user id = sa; password= 123456;");
                con.Open();

                SqlCommand cmd = new SqlCommand(sqlRestore, con);
                cmd.ExecuteNonQuery();
                con.Close();
                MessageBox.Show("Database đã đươck restore ");

            }
            catch (SqlException ex)
            {
                MessageBox.Show(ex.Message, "Restore Database");
                return;
            }
        }
    }
}
